package com.ruoyi.finance.service;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.experiment.domain.Circulation;
import com.ruoyi.experiment.domain.Experiment;
import com.ruoyi.experiment.domain.Sample;
import com.ruoyi.experiment.mapper.ExperimentMapper;
import com.ruoyi.experiment.utils.Num;
import com.ruoyi.finance.domain.FPayment;
import com.ruoyi.finance.mapper.FinanceMapper;
import com.ruoyi.finance.model.FExperiment;
import com.ruoyi.finance.model.Pfinance;
import com.ruoyi.finance.model.Query;
import com.ruoyi.message.mapper.MessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class FinanceServiceImpl implements FinanceService{

    @Autowired
    FinanceMapper mapper;
    @Autowired
    ExperimentMapper expMapper;
    @Autowired
    MessageMapper messageMapper;

    @Override
    public List<Pfinance> getAllItem(Pfinance pro) {
        List<Pfinance> pfinances = mapper.selectItem(pro);
        for (Pfinance pfinance : pfinances) {
            List<FPayment> fPayments = mapper.selectPaymentByPid(pfinance.getProjectId());
            long hasPay=0;
            long sum=0;
            int count=0;
            int n=1;
            for (FPayment fPayment : fPayments) {
                fPayment.setNum(n++);
                sum+=fPayment.getPaymentPrice();
                if (fPayment.getAccountPrice()!=null){
                    hasPay+=fPayment.getAccountPrice();
                    count++;
                }
            }
            pfinance.setPay(fPayments);
            pfinance.setHasPay(hasPay);
            pfinance.setUnPay(sum-hasPay);
            pfinance.setHasPayCount(count);

            boolean flag = getType(pfinance.getProjectId());
            pfinance.setType(flag);
        }
        return pfinances;
    }

    @Override
    public List<FPayment> getPay(FPayment payment) {
        mapper.getPay(payment.getPaymentId(), payment.getAccountPrice()==null?0:payment.getAccountPrice(),payment.getRemark());
        int count=1;
        List<FPayment> fPayments = mapper.selectPaymentByPid(payment.getProjectId());
        for (FPayment fPayment : fPayments) {
            fPayment.setNum(count++);
        }
        return fPayments;
    }

    @Override
    public List<FPayment> backPay(Long id,Long pid) {
        mapper.backPay(id);
        int count=1;
        List<FPayment> fPayments = mapper.selectPaymentByPid(pid);
        for (FPayment fPayment : fPayments) {
            fPayment.setNum(count++);
        }
        return fPayments;
    }

    @Override
    public List<Experiment> getExpByProjectId(Query query, Integer status) {
        if (status==5){
            return mapper.getExpByProjectId5(query);
        }else
            return mapper.getExpByProjectId4(query);
    }

    @Override
    @Transactional
    public void check(Long id) {
        Experiment experiment = expMapper.selectExperimentalManagementByExperimentId(id);
        Integer expermentType = experiment.getExpermentType();
        mapper.changeStatus(id,Num.set(expermentType,2,5));
        mapper.updateProjectTypeToF(id);
    }

    @Override
    public void back(Long id) {
        Experiment experiment = expMapper.selectExperimentalManagementByExperimentId(id);
        Integer expermentType = experiment.getExpermentType();
        mapper.changeStatus(id,Num.set(expermentType,2,3));
        Long pid = expMapper.selectProjectId(experiment.getSampleId());
        Circulation circulation = new Circulation();
        circulation.setCirculationType(8);
        circulation.setProjectId(pid);
        circulation.setUserName(SecurityUtils.getUsername());
        circulation.setCirculationSuggestion("岗位结算信息退回至项目负责人");
        expMapper.insertFlume(circulation);
    }

    @Override
    public Pfinance getPaymentByPid(Long pid) {
        List<FPayment> fPayments = mapper.selectPaymentByPid(pid);
        long hasPay=0;
        long sum=0;
        for (FPayment fPayment : fPayments) {
            sum+=fPayment.getPaymentPrice();
            if (fPayment.getGetDate()!=null){
                hasPay+=fPayment.getAccountPrice();
            }
        }
        Pfinance result = new Pfinance();
        result.setProjectId(pid);
        result.setPay(fPayments);
        result.setHasPay(hasPay);
        result.setUnPay(sum-hasPay);

        boolean flag = getType(pid);
        result.setType(flag);
        return result;
    }

    @Override
    public List<FExperiment> getAllExpByPid(FExperiment fe) {
        return mapper.getAllExpByPid(fe);
    }

    @Override
    public void finishPrice(Long pid,Integer finish) {
        mapper.finishPrice(pid,finish);
    }

    public boolean getType(Long pid){
        if(mapper.getProjectStyle(pid)!=5){
            return false;
        }
        if(mapper.countPaymentByPid(pid)!=0){
            return false;
        }
        if(mapper.countExpByProjectId4(pid)!=0){
            return false;
        }
        return true;
    }
}
